import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import os
import cv2
import warnings
import cv2 as cv
from glob import glob
from termcolor import colored
warnings.filterwarnings('ignore')
import itertools
import PIL.Image as Image
from tqdm import tqdm
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow import keras
from keras.utils import np_utils
from tensorflow.keras import layers
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.activations import softmax
from tensorflow.keras.utils import to_categorical
from tensorflow.keras import optimizers,regularizers
from tensorflow.python.keras.models import Sequential
from tensorflow.keras.layers import Reshape, Activation
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras.layers import Dropout,BatchNormalization, Activation
from tensorflow.keras.layers import Flatten, Dense, Conv2D, MaxPool2D
from tensorflow.python.keras.wrappers.scikit_learn import KerasRegressor
from sklearn import svm
from sklearn.svm import SVC
from sklearn import metrics
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
print(tf.__version__)
2.4.1
from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive
print(colored('\x1B[1mImoprting dataset from drive.....','blue'))
pred1 = '/content/drive/MyDrive/Files/Part !/Part-1 - Plant Seedling Classification Data/Seedling - Prediction'
train = '/content/drive/MyDrive/Files/Part !/Part-1 - Plant Seedling Classification Data/train'
Imoprting dataset from drive.....
n_train = os.listdir(train)
n_pred = os.listdir(pred1)
print(colored('\x1B[1mGetting details of train dataset.....','blue'))
print('\x1B[1mTotal no of folders inside train dataset are',len(n_train))
print('\x1B[1mWhich are as follows:\n',n_train)
Getting details of train dataset..... Total no of folders inside train dataset are 12 Which are as follows: ['Black-grass', 'Charlock', 'Cleavers', 'Common Chickweed', 'Common wheat', 'Fat Hen', 'Loose Silky-bent', 'Maize', 'Scentless Mayweed', 'Shepherds Purse', 'Small-flowered Cranesbill', 'Sugar beet']
print('\x1B[1mGetiing total no of imgaes from each folders','blue')
for folder in os.listdir(train):
size=[]
for image in os.listdir(train + '/' + folder):
size.append(os.path.join(train, folder, image))
print('\x1B[1mNo of images in',folder,':',len(size))
Getiing total no of imgaes from each folders blue No of images in Black-grass : 263 No of images in Charlock : 390 No of images in Cleavers : 287 No of images in Common Chickweed : 611 No of images in Common wheat : 221 No of images in Fat Hen : 475 No of images in Loose Silky-bent : 671 No of images in Maize : 221 No of images in Scentless Mayweed : 516 No of images in Shepherds Purse : 231 No of images in Small-flowered Cranesbill : 496 No of images in Sugar beet : 385
classes = {}
for class_name in os.listdir(train):
classes[class_name] = len(os.listdir(os.path.join(train, class_name)))
plt.bar(classes.keys(), classes.values())
plt.xticks(rotation=90)
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], <a list of 12 Text major ticklabel objects>)
print(colored('\x1B[1mGetting random images from each folder of train dataset.....','blue'))
Getting random images from each folder of train dataset.....
import random
from PIL import Image
from skimage.io import imread
imagee = [[] for x in range(1,13)]
label=[]
i=0
for folder in os.listdir(train):
label.append(folder)
for image in os.listdir(train + '/' + folder):
imagee[i].append(os.path.join(train, folder, image))
i=i+1
n = 0
i=0
for x in range(1,13):
print(colored('\x1B[1mFolder Name','green'))
print('\x1B[1m',label[i])
plt.figure(figsize=(100, 100))
plt.axis('off')
for y in range(3):
n += 1
random_img = random.choice(imagee[i])
imgs = imread(random_img)
plt.subplot(18,18, n)
plt.imshow(imgs)
i += 1
plt.show()
Folder Name Black-grass
Folder Name Charlock
Folder Name Cleavers
Folder Name Common Chickweed
Folder Name Common wheat
Folder Name Fat Hen
Folder Name Loose Silky-bent
Folder Name Maize
Folder Name Scentless Mayweed
Folder Name Shepherds Purse
Folder Name Small-flowered Cranesbill
Folder Name Sugar beet
images_path = '/content/drive/MyDrive/Files/Part !/Part-1 - Plant Seedling Classification Data/train/*/*.png'
print(colored('\x1B[1mSeperating Classes and Images of train dataset','blue'))
images = glob(images_path)
train_images = []
train_labels = []
for img in images:
train_images.append(cv2.resize(cv2.imread(img), (64, 64)))
train_labels.append(img.split('/')[-2])
train_X = np.asarray(train_images)
train_Y = pd.DataFrame(train_labels)
Seperating Classes and Images of train dataset
print(colored('\x1B[1mTotal numbers Images of train dataset','blue'))
print('\x1B[1m',len(train_X))
Total numbers Images of train dataset 4767
print(colored('\x1B[1mShape of Images','blue'))
print('\x1B[1m',train_X.shape)
Shape of Images (4767, 64, 64, 3)
print(colored('\x1B[1mShape of Label','blue'))
print('\x1B[1m',train_Y.shape)
Shape of Label (4767, 1)
print(colored('\x1B[1mLabel encoding of Classes','blue'))
Label encoding of Classes
labels = LabelEncoder()
labels.fit(train_Y[0])
print('Classes'+str(labels.classes_))
Classes['Black-grass' 'Charlock' 'Cleavers' 'Common Chickweed' 'Common wheat' 'Fat Hen' 'Loose Silky-bent' 'Maize' 'Scentless Mayweed' 'Shepherds Purse' 'Small-flowered Cranesbill' 'Sugar beet']
y = labels.transform(train_Y[0])
classes = y.shape
print(str(classes))
(4767,)
#target_dict={k: v for v, k in enumerate(np.unique(train_Y[0]))}
#print(colored('\x1B[1mClasses : Labels','green'))
#target_dict
#target_val= [target_dict[train_Y[0][i]] for i in range(len(train_Y[0]))]
#print(colored('\x1B[1mUnique labses','blue'))
#np.unique(target_val)
print(colored('\x1B[1mLength of total image set','blue'))
lenofimage = len(train_X)
lenofimage
Length of total image set
4767
print(colored('\x1B[1mReshaping of image dataset','blue'))
x = np.array(train_X).reshape(lenofimage,-1)
print('\x1B[1m',x.shape)
Reshaping of image dataset (4767, 12288)
print(colored('\x1B[1mMinimun and maximun Pixels of image','blue'))
print('Max' , x.max())
print('Min' , x.min())
Minimun and maximun Pixels of image
Max 255
Min 0
print(colored('\x1B[1mNormalzing','green'))
x = x/255
Normalzing
print(colored('\x1B[1mMinimun and maximun Pixels of image','blue'))
print(colored('\x1B[1mAfter Normalzation','green'))
print('Max' , x.max())
print('Min' , x.min())
Minimun and maximun Pixels of image After Normalzation Max 1.0 Min 0.0
print(colored('\x1B[1mChanging target labels from list to array','blue'))
#y=np.array(target_val)
print('\x1B[1m',y.shape)
Changing target labels from list to array (4767,)
print(colored('\x1B[1mSpliting','blue'))
X_train, X_test, y_train, y_test = train_test_split(x, y, train_size=0.7, test_size=0.3, random_state=42)
score_train={}
score_test={}
accuracy={}
print(colored('\x1B[1mShape of datasets','green'))
print('Shape of X Train',X_train.shape)
print('Shape of Y Train',y_train.shape)
print('Shape of X Test',X_test.shape)
print('Shape of Y Test',y_test.shape)
Spliting Shape of datasets Shape of X Train (3336, 12288) Shape of Y Train (3336,) Shape of X Test (1431, 12288) Shape of Y Test (1431,)
print(colored('\x1B[1mSVM','blue'))
svm = SVC(kernel='linear', gamma= 'auto',C=.1)
SVM
print(colored('\x1B[1mFitting Model','blue'))
svm.fit(X_train, y_train)
Fitting Model
SVC(C=0.1, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
print(colored('\x1B[1mPredicition','blue'))
y_pred = svm.predict(X_test)
model_train = svm.score(X_train,y_train)
model_test = svm.score(X_test,y_test)
score_train['SVM'] = model_train
score_test['SVM Test Score']=model_test
accuracy['SVM Accuracy'] = metrics.accuracy_score(y_test, y_pred)
Predicition
print(colored('\x1B[1mAccuracy','blue'))
print('Train Score:',model_train)
print('Test Score:',model_test)
Accuracy
Train Score: 0.9961031175059952
Test Score: 0.4996505939902166
print(colored('\x1B[1mClassification Report','blue'))
print(classification_report(y_test, y_pred))
cm_svc = (y_test != y_pred).sum()
print('\n Misclassified samples in SVM: {}'.format(cm_svc))
Classification Report
precision recall f1-score support
0 0.32 0.24 0.27 89
1 0.60 0.64 0.62 120
2 0.58 0.57 0.57 86
3 0.54 0.64 0.59 190
4 0.17 0.20 0.18 59
5 0.41 0.47 0.44 137
6 0.50 0.64 0.56 196
7 0.69 0.28 0.40 72
8 0.45 0.52 0.48 143
9 0.38 0.32 0.35 62
10 0.70 0.66 0.68 154
11 0.49 0.24 0.33 123
accuracy 0.50 1431
macro avg 0.49 0.45 0.46 1431
weighted avg 0.51 0.50 0.49 1431
Misclassified samples in SVM: 716
print(colored('\x1B[1mRandom Forest','blue'))
rf = RandomForestClassifier(n_estimators= 25, max_features= 6, max_depth= 10, bootstrap= False)
Random Forest
print(colored('\x1B[1mFitting Model','blue'))
rf.fit(X_train,y_train)
Fitting Model
RandomForestClassifier(bootstrap=False, ccp_alpha=0.0, class_weight=None,
criterion='gini', max_depth=10, max_features=6,
max_leaf_nodes=None, max_samples=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=25,
n_jobs=None, oob_score=False, random_state=None,
verbose=0, warm_start=False)
print(colored('\x1B[1mPredicition','blue'))
y_pred = rf.predict(X_test)
model_train = rf.score(X_train,y_train)
model_test = rf.score(X_test,y_test)
score_train['Random Forest'] = model_train
score_test['Random Forest Test Score']=model_test
accuracy['Random Forest Accuracy'] = metrics.accuracy_score(y_test, y_pred)
Predicition
print(colored('\x1B[1mAccuracy','blue'))
print('Train Score:',model_train)
print('Test Score:',model_test)
Accuracy
Train Score: 0.9589328537170264
Test Score: 0.4276729559748428
print(colored('\x1B[1mClassification Report','blue'))
print(classification_report(y_test, y_pred))
cm_rf = (y_test != y_pred).sum()
print('\n Misclassified samples in Random Forest: {}'.format(cm_rf))
Classification Report
precision recall f1-score support
0 0.67 0.04 0.08 89
1 0.56 0.40 0.47 120
2 0.58 0.29 0.39 86
3 0.51 0.76 0.61 190
4 0.33 0.02 0.03 59
5 0.34 0.20 0.26 137
6 0.33 0.87 0.48 196
7 0.82 0.12 0.22 72
8 0.42 0.56 0.48 143
9 0.00 0.00 0.00 62
10 0.50 0.61 0.55 154
11 0.50 0.07 0.12 123
accuracy 0.43 1431
macro avg 0.46 0.33 0.31 1431
weighted avg 0.46 0.43 0.37 1431
Misclassified samples in Random Forest: 819
print(colored('\x1B[1mCDecision Tree','blue'))
dTree = DecisionTreeClassifier(min_samples_leaf=7, max_features=5, max_depth=5, criterion='gini', random_state=1)
CDecision Tree
print(colored('\x1B[1mFitting Model','blue'))
dTree.fit(X_train,y_train)
Fitting Model
DecisionTreeClassifier(ccp_alpha=0.0, class_weight=None, criterion='gini',
max_depth=5, max_features=5, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=7, min_samples_split=2,
min_weight_fraction_leaf=0.0, presort='deprecated',
random_state=1, splitter='best')
print(colored('\x1B[1mPredicition','blue'))
y_pred = dTree.predict(X_test)
model_train = dTree.score(X_train,y_train)
model_test = dTree.score(X_test,y_test)
score_train['Decision Tree'] = model_train
score_test['Decision Tree Test Score']=model_test
accuracy['Decision Tree Accuracy'] = metrics.accuracy_score(y_test, y_pred)
Predicition
print(colored('\x1B[1mAccuracy','blue'))
print('Train Score:',model_train)
print('Test Score:',model_test)
Accuracy
Train Score: 0.2643884892086331
Test Score: 0.2236198462613557
print(colored('\x1B[1mClassification Report','blue'))
print(classification_report(y_test, y_pred))
cm_dTree = (y_test != y_pred).sum()
print('\n Misclassified samples in Decision Tree: {}'.format(cm_dTree))
Classification Report
precision recall f1-score support
0 0.00 0.00 0.00 89
1 0.20 0.02 0.03 120
2 0.00 0.00 0.00 86
3 0.37 0.32 0.34 190
4 0.00 0.00 0.00 59
5 0.10 0.04 0.06 137
6 0.20 0.74 0.31 196
7 0.26 0.10 0.14 72
8 0.18 0.34 0.23 143
9 0.00 0.00 0.00 62
10 0.31 0.32 0.32 154
11 0.00 0.00 0.00 123
accuracy 0.22 1431
macro avg 0.13 0.16 0.12 1431
weighted avg 0.17 0.22 0.16 1431
Misclassified samples in Decision Tree: 1111
print(colored('\x1B[1mSetting labels to categorically labels','blue'))
y_c = to_categorical(y, num_classes=12)
Setting labels to categorically labels
print(colored('\x1B[1mSpliting','blue'))
X_train, X_test, y_train, y_test = train_test_split(x, y_c, train_size=0.8, test_size=0.2, random_state=42)
Spliting
print(colored('\x1B[1mShape of datasets','green'))
print('Shape of X Train',X_train.shape)
print('Shape of Y Train',y_train.shape)
print('Shape of X Test',X_test.shape)
print('Shape of Y Test',y_test.shape)
Shape of datasets
Shape of X Train (3813, 12288)
Shape of Y Train (3813, 12)
Shape of X Test (954, 12288)
Shape of Y Test (954, 12)
input = x.shape[1]
input
12288
print(colored('\x1B[1mNeural Network Model Building','blue'))
Neural Network Model Building
hidden_nodes = 256
output_nodes = 12
nn_sgd = Sequential()
nn_sgd.add(Dense(hidden_nodes, input_shape=(input,), activation='relu'))
nn_sgd.add(Dense(hidden_nodes, activation='relu'))
nn_sgd.add(Dense(output_nodes, activation='softmax', kernel_regularizer=regularizers.l2(0)))
print(colored('\x1B[1mCompiling Model Using SGD Optimizer','blue'))
sgd = optimizers.SGD(lr=.001, decay=1e-6, momentum=0.9)
nn_sgd.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
Compiling Model Using SGD Optimizer
print(colored('\x1B[1mFitting Model Using SGD Optimizer','blue'))
Fitting Model Using SGD Optimizer
nn_sgd.fit(X_train, y_train, epochs=100, batch_size=200, verbose= 1)
score = nn_sgd.evaluate(X_test, y_test, verbose=0)
score_train['NN(SGD)'] = nn_sgd.evaluate(X_train, y_train, verbose=0)
score_test['NN(SGD) Test Score']=score
accuracy['NN(SGD) Accuracy'] = score
Epoch 1/100 20/20 [==============================] - 2s 69ms/step - loss: 2.4953 - accuracy: 0.1348 Epoch 2/100 20/20 [==============================] - 1s 75ms/step - loss: 2.4016 - accuracy: 0.1637 Epoch 3/100 20/20 [==============================] - 2s 85ms/step - loss: 2.3854 - accuracy: 0.1758 Epoch 4/100 20/20 [==============================] - 2s 77ms/step - loss: 2.3631 - accuracy: 0.1844 Epoch 5/100 20/20 [==============================] - 2s 79ms/step - loss: 2.3549 - accuracy: 0.1942 Epoch 6/100 20/20 [==============================] - 2s 91ms/step - loss: 2.3278 - accuracy: 0.2308 Epoch 7/100 20/20 [==============================] - 2s 94ms/step - loss: 2.3116 - accuracy: 0.2218 Epoch 8/100 20/20 [==============================] - 2s 89ms/step - loss: 2.2991 - accuracy: 0.2487 Epoch 9/100 20/20 [==============================] - 2s 97ms/step - loss: 2.2730 - accuracy: 0.2410 Epoch 10/100 20/20 [==============================] - 2s 80ms/step - loss: 2.2665 - accuracy: 0.2646 Epoch 11/100 20/20 [==============================] - 2s 88ms/step - loss: 2.2390 - accuracy: 0.2572 Epoch 12/100 20/20 [==============================] - 2s 74ms/step - loss: 2.2312 - accuracy: 0.2588 Epoch 13/100 20/20 [==============================] - 1s 66ms/step - loss: 2.1925 - accuracy: 0.2934 Epoch 14/100 20/20 [==============================] - 1s 67ms/step - loss: 2.1931 - accuracy: 0.2531 Epoch 15/100 20/20 [==============================] - 1s 66ms/step - loss: 2.1741 - accuracy: 0.3064 Epoch 16/100 20/20 [==============================] - 1s 67ms/step - loss: 2.1579 - accuracy: 0.3089 Epoch 17/100 20/20 [==============================] - 1s 67ms/step - loss: 2.1377 - accuracy: 0.2940 Epoch 18/100 20/20 [==============================] - 1s 67ms/step - loss: 2.1174 - accuracy: 0.2964 Epoch 19/100 20/20 [==============================] - 1s 68ms/step - loss: 2.0909 - accuracy: 0.3009 Epoch 20/100 20/20 [==============================] - 1s 67ms/step - loss: 2.0744 - accuracy: 0.3084 Epoch 21/100 20/20 [==============================] - 1s 69ms/step - loss: 2.0502 - accuracy: 0.3556 Epoch 22/100 20/20 [==============================] - 1s 67ms/step - loss: 2.0338 - accuracy: 0.3534 Epoch 23/100 20/20 [==============================] - 1s 66ms/step - loss: 2.0060 - accuracy: 0.3352 Epoch 24/100 20/20 [==============================] - 1s 68ms/step - loss: 1.9850 - accuracy: 0.3545 Epoch 25/100 20/20 [==============================] - 1s 66ms/step - loss: 1.9625 - accuracy: 0.3634 Epoch 26/100 20/20 [==============================] - 1s 66ms/step - loss: 1.9484 - accuracy: 0.3575 Epoch 27/100 20/20 [==============================] - 1s 67ms/step - loss: 1.9206 - accuracy: 0.3757 Epoch 28/100 20/20 [==============================] - 1s 69ms/step - loss: 1.9234 - accuracy: 0.3442 Epoch 29/100 20/20 [==============================] - 1s 67ms/step - loss: 1.8855 - accuracy: 0.3930 Epoch 30/100 20/20 [==============================] - 1s 68ms/step - loss: 1.8668 - accuracy: 0.3888 Epoch 31/100 20/20 [==============================] - 1s 68ms/step - loss: 1.8501 - accuracy: 0.3671 Epoch 32/100 20/20 [==============================] - 1s 67ms/step - loss: 1.8188 - accuracy: 0.4092 Epoch 33/100 20/20 [==============================] - 1s 67ms/step - loss: 1.8112 - accuracy: 0.3784 Epoch 34/100 20/20 [==============================] - 1s 68ms/step - loss: 1.7794 - accuracy: 0.4215 Epoch 35/100 20/20 [==============================] - 1s 67ms/step - loss: 1.7540 - accuracy: 0.4222 Epoch 36/100 20/20 [==============================] - 1s 66ms/step - loss: 1.7495 - accuracy: 0.4318 Epoch 37/100 20/20 [==============================] - 1s 67ms/step - loss: 1.7194 - accuracy: 0.4556 Epoch 38/100 20/20 [==============================] - 1s 66ms/step - loss: 1.7080 - accuracy: 0.4643 Epoch 39/100 20/20 [==============================] - 1s 68ms/step - loss: 1.7043 - accuracy: 0.4450 Epoch 40/100 20/20 [==============================] - 1s 66ms/step - loss: 1.6639 - accuracy: 0.4798 Epoch 41/100 20/20 [==============================] - 1s 68ms/step - loss: 1.6470 - accuracy: 0.4607 Epoch 42/100 20/20 [==============================] - 1s 67ms/step - loss: 1.6292 - accuracy: 0.4864 Epoch 43/100 20/20 [==============================] - 1s 69ms/step - loss: 1.6036 - accuracy: 0.4697 Epoch 44/100 20/20 [==============================] - 1s 68ms/step - loss: 1.5782 - accuracy: 0.4781 Epoch 45/100 20/20 [==============================] - 1s 67ms/step - loss: 1.5685 - accuracy: 0.5002 Epoch 46/100 20/20 [==============================] - 1s 67ms/step - loss: 1.5803 - accuracy: 0.4808 Epoch 47/100 20/20 [==============================] - 1s 66ms/step - loss: 1.5608 - accuracy: 0.4856 Epoch 48/100 20/20 [==============================] - 1s 66ms/step - loss: 1.5293 - accuracy: 0.5034 Epoch 49/100 20/20 [==============================] - 1s 67ms/step - loss: 1.5230 - accuracy: 0.5133 Epoch 50/100 20/20 [==============================] - 1s 67ms/step - loss: 1.4886 - accuracy: 0.5204 Epoch 51/100 20/20 [==============================] - 1s 68ms/step - loss: 1.4853 - accuracy: 0.5318 Epoch 52/100 20/20 [==============================] - 1s 67ms/step - loss: 1.4585 - accuracy: 0.5456 Epoch 53/100 20/20 [==============================] - 1s 66ms/step - loss: 1.4825 - accuracy: 0.5193 Epoch 54/100 20/20 [==============================] - 1s 68ms/step - loss: 1.4263 - accuracy: 0.5577 Epoch 55/100 20/20 [==============================] - 1s 67ms/step - loss: 1.4306 - accuracy: 0.5306 Epoch 56/100 20/20 [==============================] - 1s 68ms/step - loss: 1.3999 - accuracy: 0.5628 Epoch 57/100 20/20 [==============================] - 1s 66ms/step - loss: 1.4110 - accuracy: 0.5601 Epoch 58/100 20/20 [==============================] - 1s 68ms/step - loss: 1.3995 - accuracy: 0.5525 Epoch 59/100 20/20 [==============================] - 1s 69ms/step - loss: 1.3906 - accuracy: 0.5358 Epoch 60/100 20/20 [==============================] - 1s 66ms/step - loss: 1.3502 - accuracy: 0.5680 Epoch 61/100 20/20 [==============================] - 1s 68ms/step - loss: 1.3103 - accuracy: 0.5887 Epoch 62/100 20/20 [==============================] - 1s 67ms/step - loss: 1.3336 - accuracy: 0.5634 Epoch 63/100 20/20 [==============================] - 1s 69ms/step - loss: 1.3177 - accuracy: 0.5620 Epoch 64/100 20/20 [==============================] - 1s 67ms/step - loss: 1.3360 - accuracy: 0.5557 Epoch 65/100 20/20 [==============================] - 1s 68ms/step - loss: 1.2880 - accuracy: 0.5897 Epoch 66/100 20/20 [==============================] - 1s 68ms/step - loss: 1.3079 - accuracy: 0.5626 Epoch 67/100 20/20 [==============================] - 1s 68ms/step - loss: 1.2862 - accuracy: 0.5927 Epoch 68/100 20/20 [==============================] - 1s 66ms/step - loss: 1.2471 - accuracy: 0.5988 Epoch 69/100 20/20 [==============================] - 1s 67ms/step - loss: 1.2611 - accuracy: 0.6022 Epoch 70/100 20/20 [==============================] - 1s 66ms/step - loss: 1.2345 - accuracy: 0.6059 Epoch 71/100 20/20 [==============================] - 1s 67ms/step - loss: 1.2414 - accuracy: 0.6153 Epoch 72/100 20/20 [==============================] - 1s 69ms/step - loss: 1.2243 - accuracy: 0.6130 Epoch 73/100 20/20 [==============================] - 1s 67ms/step - loss: 1.2039 - accuracy: 0.6033 Epoch 74/100 20/20 [==============================] - 1s 69ms/step - loss: 1.1976 - accuracy: 0.6258 Epoch 75/100 20/20 [==============================] - 1s 68ms/step - loss: 1.1859 - accuracy: 0.6187 Epoch 76/100 20/20 [==============================] - 1s 67ms/step - loss: 1.1939 - accuracy: 0.6155 Epoch 77/100 20/20 [==============================] - 1s 66ms/step - loss: 1.1608 - accuracy: 0.6454 Epoch 78/100 20/20 [==============================] - 1s 67ms/step - loss: 1.1507 - accuracy: 0.6409 Epoch 79/100 20/20 [==============================] - 1s 67ms/step - loss: 1.1386 - accuracy: 0.6440 Epoch 80/100 20/20 [==============================] - 1s 66ms/step - loss: 1.1183 - accuracy: 0.6576 Epoch 81/100 20/20 [==============================] - 1s 67ms/step - loss: 1.1076 - accuracy: 0.6658 Epoch 82/100 20/20 [==============================] - 1s 67ms/step - loss: 1.1094 - accuracy: 0.6642 Epoch 83/100 20/20 [==============================] - 1s 67ms/step - loss: 1.1435 - accuracy: 0.6246 Epoch 84/100 20/20 [==============================] - 1s 67ms/step - loss: 1.0996 - accuracy: 0.6551 Epoch 85/100 20/20 [==============================] - 1s 67ms/step - loss: 1.1018 - accuracy: 0.6417 Epoch 86/100 20/20 [==============================] - 1s 68ms/step - loss: 1.0779 - accuracy: 0.6615 Epoch 87/100 20/20 [==============================] - 1s 69ms/step - loss: 1.1202 - accuracy: 0.6169 Epoch 88/100 20/20 [==============================] - 1s 68ms/step - loss: 1.0727 - accuracy: 0.6720 Epoch 89/100 20/20 [==============================] - 1s 67ms/step - loss: 1.0874 - accuracy: 0.6334 Epoch 90/100 20/20 [==============================] - 1s 67ms/step - loss: 1.0548 - accuracy: 0.6738 Epoch 91/100 20/20 [==============================] - 1s 66ms/step - loss: 1.0263 - accuracy: 0.6743 Epoch 92/100 20/20 [==============================] - 1s 67ms/step - loss: 1.0284 - accuracy: 0.6688 Epoch 93/100 20/20 [==============================] - 1s 67ms/step - loss: 0.9980 - accuracy: 0.6903 Epoch 94/100 20/20 [==============================] - 1s 66ms/step - loss: 0.9948 - accuracy: 0.6909 Epoch 95/100 20/20 [==============================] - 1s 68ms/step - loss: 0.9704 - accuracy: 0.7062 Epoch 96/100 20/20 [==============================] - 1s 67ms/step - loss: 1.0265 - accuracy: 0.6673 Epoch 97/100 20/20 [==============================] - 1s 68ms/step - loss: 1.0043 - accuracy: 0.6783 Epoch 98/100 20/20 [==============================] - 1s 67ms/step - loss: 0.9969 - accuracy: 0.6876 Epoch 99/100 20/20 [==============================] - 1s 67ms/step - loss: 0.9652 - accuracy: 0.6864 Epoch 100/100 20/20 [==============================] - 1s 67ms/step - loss: 0.9836 - accuracy: 0.6717
print(colored('\x1B[1mScore','green'),score)
Score [1.4598736763000488, 0.5073375105857849]
hidden_nodes = 256
output_nodes = 12
nn_adam = Sequential()
nn_adam.add(Dense(hidden_nodes, input_shape=(input,), activation='relu'))
nn_adam.add(Dense(hidden_nodes, activation='relu'))
nn_adam.add(Dense(output_nodes, activation='softmax', kernel_regularizer=regularizers.l2(0)))
print(colored('\x1B[1mCompiling Model Using ADAM Optimizer','blue'))
opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.001, amsgrad=False)
nn_adam.compile(optimizer = opt, loss = 'categorical_crossentropy', metrics = ['accuracy'])
Compiling Model Using ADAM Optimizer
print(colored('\x1B[1mFitting Model Using ADAM Optimizer','blue'))
Fitting Model Using ADAM Optimizer
nn_adam.fit(X_train, y_train, epochs=100, batch_size=200, verbose= 1)
score = nn_adam.evaluate(X_test, y_test, verbose=0)
score_train['NN(ADAM)'] = nn_adam.evaluate(X_train, y_train, verbose=0)
score_test['NN Test Score']=score
accuracy['NN Accuracy'] = score
Epoch 1/100 20/20 [==============================] - 2s 71ms/step - loss: 3.6157 - accuracy: 0.1136 Epoch 2/100 20/20 [==============================] - 1s 72ms/step - loss: 2.4529 - accuracy: 0.1326 Epoch 3/100 20/20 [==============================] - 1s 71ms/step - loss: 2.3846 - accuracy: 0.1802 Epoch 4/100 20/20 [==============================] - 1s 70ms/step - loss: 2.3551 - accuracy: 0.1613 Epoch 5/100 20/20 [==============================] - 1s 71ms/step - loss: 2.2313 - accuracy: 0.2386 Epoch 6/100 20/20 [==============================] - 1s 70ms/step - loss: 2.1137 - accuracy: 0.2815 Epoch 7/100 20/20 [==============================] - 1s 70ms/step - loss: 2.0997 - accuracy: 0.2442 Epoch 8/100 20/20 [==============================] - 1s 71ms/step - loss: 1.9709 - accuracy: 0.3301 Epoch 9/100 20/20 [==============================] - 1s 69ms/step - loss: 1.9637 - accuracy: 0.3265 Epoch 10/100 20/20 [==============================] - 1s 69ms/step - loss: 1.8187 - accuracy: 0.3911 Epoch 11/100 20/20 [==============================] - 1s 70ms/step - loss: 1.7756 - accuracy: 0.3989 Epoch 12/100 20/20 [==============================] - 1s 68ms/step - loss: 1.7380 - accuracy: 0.3934 Epoch 13/100 20/20 [==============================] - 1s 69ms/step - loss: 1.6948 - accuracy: 0.4015 Epoch 14/100 20/20 [==============================] - 1s 70ms/step - loss: 1.6987 - accuracy: 0.3790 Epoch 15/100 20/20 [==============================] - 1s 70ms/step - loss: 1.5841 - accuracy: 0.4599 Epoch 16/100 20/20 [==============================] - 1s 69ms/step - loss: 1.4903 - accuracy: 0.4963 Epoch 17/100 20/20 [==============================] - 1s 71ms/step - loss: 1.6217 - accuracy: 0.4249 Epoch 18/100 20/20 [==============================] - 1s 71ms/step - loss: 1.4456 - accuracy: 0.5085 Epoch 19/100 20/20 [==============================] - 1s 70ms/step - loss: 1.4961 - accuracy: 0.4865 Epoch 20/100 20/20 [==============================] - 1s 69ms/step - loss: 1.3324 - accuracy: 0.5608 Epoch 21/100 20/20 [==============================] - 1s 68ms/step - loss: 1.2951 - accuracy: 0.5615 Epoch 22/100 20/20 [==============================] - 1s 69ms/step - loss: 1.3330 - accuracy: 0.5269 Epoch 23/100 20/20 [==============================] - 1s 71ms/step - loss: 1.2664 - accuracy: 0.5614 Epoch 24/100 20/20 [==============================] - 1s 69ms/step - loss: 1.2051 - accuracy: 0.6016 Epoch 25/100 20/20 [==============================] - 1s 69ms/step - loss: 1.2675 - accuracy: 0.5499 Epoch 26/100 20/20 [==============================] - 1s 70ms/step - loss: 1.2458 - accuracy: 0.5546 Epoch 27/100 20/20 [==============================] - 1s 68ms/step - loss: 1.2803 - accuracy: 0.5324 Epoch 28/100 20/20 [==============================] - 1s 69ms/step - loss: 1.2054 - accuracy: 0.5788 Epoch 29/100 20/20 [==============================] - 1s 70ms/step - loss: 1.0915 - accuracy: 0.6314 Epoch 30/100 20/20 [==============================] - 1s 71ms/step - loss: 1.0891 - accuracy: 0.6306 Epoch 31/100 20/20 [==============================] - 1s 70ms/step - loss: 1.0302 - accuracy: 0.6514 Epoch 32/100 20/20 [==============================] - 1s 69ms/step - loss: 0.9949 - accuracy: 0.6721 Epoch 33/100 20/20 [==============================] - 1s 70ms/step - loss: 1.0147 - accuracy: 0.6534 Epoch 34/100 20/20 [==============================] - 1s 70ms/step - loss: 0.9788 - accuracy: 0.6743 Epoch 35/100 20/20 [==============================] - 1s 68ms/step - loss: 0.9478 - accuracy: 0.6915 Epoch 36/100 20/20 [==============================] - 1s 69ms/step - loss: 0.9492 - accuracy: 0.6786 Epoch 37/100 20/20 [==============================] - 1s 71ms/step - loss: 1.0141 - accuracy: 0.6357 Epoch 38/100 20/20 [==============================] - 1s 70ms/step - loss: 0.9619 - accuracy: 0.6648 Epoch 39/100 20/20 [==============================] - 1s 68ms/step - loss: 0.9828 - accuracy: 0.6615 Epoch 40/100 20/20 [==============================] - 1s 70ms/step - loss: 0.8419 - accuracy: 0.7332 Epoch 41/100 20/20 [==============================] - 1s 68ms/step - loss: 0.8877 - accuracy: 0.6989 Epoch 42/100 20/20 [==============================] - 1s 69ms/step - loss: 0.7982 - accuracy: 0.7482 Epoch 43/100 20/20 [==============================] - 1s 71ms/step - loss: 0.8494 - accuracy: 0.7166 Epoch 44/100 20/20 [==============================] - 1s 69ms/step - loss: 0.8278 - accuracy: 0.7361 Epoch 45/100 20/20 [==============================] - 1s 71ms/step - loss: 0.7907 - accuracy: 0.7431 Epoch 46/100 20/20 [==============================] - 1s 69ms/step - loss: 0.8065 - accuracy: 0.7263 Epoch 47/100 20/20 [==============================] - 1s 69ms/step - loss: 0.8037 - accuracy: 0.7439 Epoch 48/100 20/20 [==============================] - 1s 69ms/step - loss: 0.7599 - accuracy: 0.7542 Epoch 49/100 20/20 [==============================] - 1s 71ms/step - loss: 0.7791 - accuracy: 0.7432 Epoch 50/100 20/20 [==============================] - 1s 69ms/step - loss: 0.7711 - accuracy: 0.7474 Epoch 51/100 20/20 [==============================] - 1s 70ms/step - loss: 0.7134 - accuracy: 0.7716 Epoch 52/100 20/20 [==============================] - 1s 69ms/step - loss: 0.7395 - accuracy: 0.7509 Epoch 53/100 20/20 [==============================] - 1s 70ms/step - loss: 0.7052 - accuracy: 0.7806 Epoch 54/100 20/20 [==============================] - 1s 70ms/step - loss: 0.6933 - accuracy: 0.7769 Epoch 55/100 20/20 [==============================] - 1s 69ms/step - loss: 0.7746 - accuracy: 0.7246 Epoch 56/100 20/20 [==============================] - 1s 70ms/step - loss: 0.6652 - accuracy: 0.7892 Epoch 57/100 20/20 [==============================] - 1s 70ms/step - loss: 0.6600 - accuracy: 0.7943 Epoch 58/100 20/20 [==============================] - 1s 72ms/step - loss: 0.6216 - accuracy: 0.8175 Epoch 59/100 20/20 [==============================] - 1s 71ms/step - loss: 0.6093 - accuracy: 0.8134 Epoch 60/100 20/20 [==============================] - 1s 69ms/step - loss: 0.6283 - accuracy: 0.8010 Epoch 61/100 20/20 [==============================] - 1s 70ms/step - loss: 0.6310 - accuracy: 0.8016 Epoch 62/100 20/20 [==============================] - 1s 70ms/step - loss: 0.6031 - accuracy: 0.8096 Epoch 63/100 20/20 [==============================] - 1s 69ms/step - loss: 0.5697 - accuracy: 0.8371 Epoch 64/100 20/20 [==============================] - 1s 71ms/step - loss: 0.6653 - accuracy: 0.7797 Epoch 65/100 20/20 [==============================] - 1s 69ms/step - loss: 0.5718 - accuracy: 0.8313 Epoch 66/100 20/20 [==============================] - 1s 71ms/step - loss: 0.6031 - accuracy: 0.8164 Epoch 67/100 20/20 [==============================] - 1s 70ms/step - loss: 0.6216 - accuracy: 0.7900 Epoch 68/100 20/20 [==============================] - 1s 69ms/step - loss: 0.5554 - accuracy: 0.8277 Epoch 69/100 20/20 [==============================] - 1s 73ms/step - loss: 0.5220 - accuracy: 0.8408 Epoch 70/100 20/20 [==============================] - 1s 70ms/step - loss: 0.5010 - accuracy: 0.8524 Epoch 71/100 20/20 [==============================] - 1s 71ms/step - loss: 0.5524 - accuracy: 0.8252 Epoch 72/100 20/20 [==============================] - 1s 70ms/step - loss: 0.5036 - accuracy: 0.8505 Epoch 73/100 20/20 [==============================] - 1s 71ms/step - loss: 0.5122 - accuracy: 0.8370 Epoch 74/100 20/20 [==============================] - 1s 71ms/step - loss: 0.4898 - accuracy: 0.8495 Epoch 75/100 20/20 [==============================] - 1s 71ms/step - loss: 0.4524 - accuracy: 0.8786 Epoch 76/100 20/20 [==============================] - 1s 70ms/step - loss: 0.4848 - accuracy: 0.8617 Epoch 77/100 20/20 [==============================] - 1s 70ms/step - loss: 0.4798 - accuracy: 0.8611 Epoch 78/100 20/20 [==============================] - 1s 69ms/step - loss: 0.4656 - accuracy: 0.8654 Epoch 79/100 20/20 [==============================] - 1s 72ms/step - loss: 0.4926 - accuracy: 0.8481 Epoch 80/100 20/20 [==============================] - 1s 73ms/step - loss: 0.5299 - accuracy: 0.8291 Epoch 81/100 20/20 [==============================] - 1s 71ms/step - loss: 0.4649 - accuracy: 0.8775 Epoch 82/100 20/20 [==============================] - 1s 71ms/step - loss: 0.4097 - accuracy: 0.8877 Epoch 83/100 20/20 [==============================] - 1s 70ms/step - loss: 0.4409 - accuracy: 0.8707 Epoch 84/100 20/20 [==============================] - 1s 72ms/step - loss: 0.4433 - accuracy: 0.8719 Epoch 85/100 20/20 [==============================] - 1s 70ms/step - loss: 0.4039 - accuracy: 0.8942 Epoch 86/100 20/20 [==============================] - 1s 72ms/step - loss: 0.4237 - accuracy: 0.8753 Epoch 87/100 20/20 [==============================] - 1s 70ms/step - loss: 0.4851 - accuracy: 0.8494 Epoch 88/100 20/20 [==============================] - 1s 69ms/step - loss: 0.5060 - accuracy: 0.8289 Epoch 89/100 20/20 [==============================] - 1s 71ms/step - loss: 0.4024 - accuracy: 0.8805 Epoch 90/100 20/20 [==============================] - 1s 70ms/step - loss: 0.3918 - accuracy: 0.8828 Epoch 91/100 20/20 [==============================] - 1s 72ms/step - loss: 0.3782 - accuracy: 0.9016 Epoch 92/100 20/20 [==============================] - 1s 72ms/step - loss: 0.3629 - accuracy: 0.9091 Epoch 93/100 20/20 [==============================] - 1s 71ms/step - loss: 0.3524 - accuracy: 0.9146 Epoch 94/100 20/20 [==============================] - 1s 73ms/step - loss: 0.3468 - accuracy: 0.9145 Epoch 95/100 20/20 [==============================] - 1s 72ms/step - loss: 0.3539 - accuracy: 0.9099 Epoch 96/100 20/20 [==============================] - 1s 73ms/step - loss: 0.3382 - accuracy: 0.9169 Epoch 97/100 20/20 [==============================] - 1s 70ms/step - loss: 0.3594 - accuracy: 0.9004 Epoch 98/100 20/20 [==============================] - 1s 71ms/step - loss: 0.3392 - accuracy: 0.9122 Epoch 99/100 20/20 [==============================] - 1s 69ms/step - loss: 0.3170 - accuracy: 0.9285 Epoch 100/100 20/20 [==============================] - 1s 69ms/step - loss: 0.3363 - accuracy: 0.9182
print(colored('\x1B[1mScore','green'),score)
Score [1.6843140125274658, 0.5094339847564697]
print(colored('\x1B[1mSpliting Train and Test sets','blue'))
X_train,X_test,y_train,y_test=train_test_split(train_X,y_c,test_size=0.2,random_state=7)
Spliting Train and Test sets
print(colored('\x1B[1mShape of datasets','green'))
print('Shape of X Train',X_train.shape)
print('Shape of Y Train',y_train.shape)
Shape of datasets
Shape of X Train (3813, 64, 64, 3)
Shape of Y Train (3813, 12)
print(colored('\x1B[1mSpliting Test and Validation sets','blue'))
X_val,X_test_new,y_val,y_test_new = train_test_split(X_test,y_test,test_size=0.2,random_state=38,stratify=y_test)
Spliting Test and Validation sets
print(colored('\x1B[1mShape of datasets','green'))
print('Shape of X_val',X_val.shape)
print('Shape of Y_val',y_val.shape)
print('Shape of X Test',X_test_new.shape)
print('Shape of Y Test',y_test_new.shape)
Shape of datasets
Shape of X_val (763, 64, 64, 3)
Shape of Y_val (763, 12)
Shape of X Test (191, 64, 64, 3)
Shape of Y Test (191, 12)
print(colored('\x1B[1mPreprocessing Image and applying augmentation','blue'))
image_generator_train = tf.keras.preprocessing.image.ImageDataGenerator(
rotation_range=20,width_shift_range=0.2,height_shift_range=0.2,
shear_range=0.3,zoom_range=0.5,horizontal_flip=True,vertical_flip=True,
brightness_range=[0.2,0.8],featurewise_center=False,samplewise_center=False,
featurewise_std_normalization=False,samplewise_std_normalization=False,
fill_mode = "reflect")
image_generator_train.fit(X_train, augment=True)
augmented_train = image_generator_train.flow(np.array(X_train),np.array(y_train))
image_generator_val = tf.keras.preprocessing.image.ImageDataGenerator(
rotation_range=20,width_shift_range=0.2,height_shift_range=0.2,
shear_range=0.3,zoom_range=0.5,horizontal_flip=True,vertical_flip=True,
brightness_range=[0.2,0.8],featurewise_center=False,samplewise_center=False,
featurewise_std_normalization=False,samplewise_std_normalization=False,
fill_mode = "reflect")
image_generator_val.fit(X_val, augment=True)
augmented_val = image_generator_val.flow(np.array(X_val),np.array(y_val))
image_generator_test = tf.keras.preprocessing.image.ImageDataGenerator(rescale = 1./255)
image_generator_test.fit(X_test)
augmented_test = image_generator_test.flow(np.array(X_test),np.array(y_test))
print(colored('\x1B[1mFitting Train set in Preprocessing and Augmentation featuers','green'))
Preprocessing Image and applying augmentation Fitting Train set in Preprocessing and Augmentation featuers
print(colored('\x1B[1mBuilding CNN Model','blue'))
Building CNN Model
cnn_sgd = Sequential()
cnn_sgd.add(tf.keras.layers.InputLayer(input_shape=(64,64,3,)))
cnn_sgd.add(tf.keras.layers.Conv2D(64, kernel_size=(3,3), activation='relu'))
cnn_sgd.add(tf.keras.layers.MaxPool2D(pool_size = (2,2)))
cnn_sgd.add(tf.keras.layers.BatchNormalization())
cnn_sgd.add(tf.keras.layers.Conv2D(64, kernel_size=(3,3), strides = (1,1), activation='relu'))
cnn_sgd.add(tf.keras.layers.MaxPool2D(pool_size = (2,2)))
cnn_sgd.add(tf.keras.layers.BatchNormalization())
cnn_sgd.add(tf.keras.layers.Conv2D(128, kernel_size=(3,3), strides = (1,1), activation='relu'))
cnn_sgd.add(tf.keras.layers.MaxPool2D(pool_size = (2,2)))
cnn_sgd.add(tf.keras.layers.BatchNormalization())
cnn_sgd.add(tf.keras.layers.Conv2D(128, kernel_size=(3,3), strides = (1,1), activation='relu'))
cnn_sgd.add(tf.keras.layers.MaxPool2D(pool_size = (2,2)))
cnn_sgd.add(tf.keras.layers.GlobalMaxPool2D())
cnn_sgd.add(tf.keras.layers.Flatten())
cnn_sgd.add(tf.keras.layers.Dense(128, activation='relu'))
cnn_sgd.add(tf.keras.layers.Dropout(0.2))
cnn_sgd.add(tf.keras.layers.Dense(64, activation='relu'))
cnn_sgd.add(tf.keras.layers.BatchNormalization())
cnn_sgd.add(tf.keras.layers.Dense(12, activation='softmax'))
print(colored('\x1B[1mCompiling Model Using SGD Optimizer','blue'))
sgd = optimizers.SGD(lr=.001, decay=1e-6, momentum=0.9)
cnn_sgd.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
Compiling Model Using SGD Optimizer
print(colored('\x1B[1mFitting Model Using SGD Optimizer','blue'))
Fitting Model Using SGD Optimizer
history1 = cnn_sgd.fit(augmented_train,epochs=50,shuffle=True,
steps_per_epoch = 70,
validation_data=augmented_val,
validation_steps = int(500/32))
score = cnn_sgd.evaluate(X_test_new, y_test_new, verbose=0)
score_train['CNN(SGD)'] = cnn_sgd.evaluate(X_train, y_train, verbose=0)
score_test['CNN(SGD) Test Score']=score
accuracy['CNN(SGD) Accuracy'] = score
Epoch 1/50 70/70 [==============================] - 31s 422ms/step - loss: 2.8754 - accuracy: 0.1056 - val_loss: 2.5048 - val_accuracy: 0.1729 Epoch 2/50 70/70 [==============================] - 29s 417ms/step - loss: 2.3584 - accuracy: 0.2099 - val_loss: 2.0380 - val_accuracy: 0.3146 Epoch 3/50 70/70 [==============================] - 29s 418ms/step - loss: 2.1266 - accuracy: 0.2812 - val_loss: 2.0462 - val_accuracy: 0.2875 Epoch 4/50 70/70 [==============================] - 29s 417ms/step - loss: 2.0397 - accuracy: 0.2819 - val_loss: 2.6113 - val_accuracy: 0.2208 Epoch 5/50 70/70 [==============================] - 30s 423ms/step - loss: 1.9194 - accuracy: 0.3245 - val_loss: 1.8213 - val_accuracy: 0.3917 Epoch 6/50 70/70 [==============================] - 29s 417ms/step - loss: 1.8275 - accuracy: 0.3750 - val_loss: 2.5301 - val_accuracy: 0.2521 Epoch 7/50 70/70 [==============================] - 29s 418ms/step - loss: 1.7961 - accuracy: 0.3921 - val_loss: 2.2580 - val_accuracy: 0.2917 Epoch 8/50 70/70 [==============================] - 29s 418ms/step - loss: 1.7167 - accuracy: 0.4277 - val_loss: 2.7009 - val_accuracy: 0.2604 Epoch 9/50 70/70 [==============================] - 29s 420ms/step - loss: 1.6953 - accuracy: 0.4010 - val_loss: 1.6451 - val_accuracy: 0.4250 Epoch 10/50 70/70 [==============================] - 29s 417ms/step - loss: 1.6340 - accuracy: 0.4516 - val_loss: 1.5790 - val_accuracy: 0.4396 Epoch 11/50 70/70 [==============================] - 30s 422ms/step - loss: 1.5688 - accuracy: 0.4704 - val_loss: 1.6604 - val_accuracy: 0.4250 Epoch 12/50 70/70 [==============================] - 29s 418ms/step - loss: 1.5601 - accuracy: 0.4859 - val_loss: 1.6319 - val_accuracy: 0.4396 Epoch 13/50 70/70 [==============================] - 30s 425ms/step - loss: 1.4740 - accuracy: 0.5015 - val_loss: 1.3564 - val_accuracy: 0.5188 Epoch 14/50 70/70 [==============================] - 29s 420ms/step - loss: 1.4346 - accuracy: 0.5181 - val_loss: 1.5714 - val_accuracy: 0.4833 Epoch 15/50 70/70 [==============================] - 30s 430ms/step - loss: 1.3894 - accuracy: 0.5258 - val_loss: 1.7420 - val_accuracy: 0.4167 Epoch 16/50 70/70 [==============================] - 30s 434ms/step - loss: 1.3804 - accuracy: 0.5355 - val_loss: 1.4279 - val_accuracy: 0.5063 Epoch 17/50 70/70 [==============================] - 30s 427ms/step - loss: 1.3193 - accuracy: 0.5629 - val_loss: 1.5320 - val_accuracy: 0.4854 Epoch 18/50 70/70 [==============================] - 29s 418ms/step - loss: 1.2830 - accuracy: 0.5600 - val_loss: 1.4637 - val_accuracy: 0.5312 Epoch 19/50 70/70 [==============================] - 29s 420ms/step - loss: 1.2869 - accuracy: 0.5555 - val_loss: 1.4007 - val_accuracy: 0.5208 Epoch 20/50 70/70 [==============================] - 30s 422ms/step - loss: 1.2391 - accuracy: 0.5865 - val_loss: 1.5078 - val_accuracy: 0.4875 Epoch 21/50 70/70 [==============================] - 30s 422ms/step - loss: 1.2550 - accuracy: 0.5724 - val_loss: 1.1548 - val_accuracy: 0.5854 Epoch 22/50 70/70 [==============================] - 30s 426ms/step - loss: 1.2713 - accuracy: 0.5651 - val_loss: 1.3348 - val_accuracy: 0.5229 Epoch 23/50 70/70 [==============================] - 30s 426ms/step - loss: 1.2211 - accuracy: 0.6027 - val_loss: 1.2448 - val_accuracy: 0.5479 Epoch 24/50 70/70 [==============================] - 29s 418ms/step - loss: 1.1791 - accuracy: 0.5872 - val_loss: 1.0264 - val_accuracy: 0.6333 Epoch 25/50 70/70 [==============================] - 30s 426ms/step - loss: 1.1611 - accuracy: 0.5967 - val_loss: 1.0965 - val_accuracy: 0.6271 Epoch 26/50 70/70 [==============================] - 30s 426ms/step - loss: 1.1690 - accuracy: 0.6131 - val_loss: 1.3625 - val_accuracy: 0.5500 Epoch 27/50 70/70 [==============================] - 29s 420ms/step - loss: 1.1192 - accuracy: 0.6194 - val_loss: 1.1505 - val_accuracy: 0.5875 Epoch 28/50 70/70 [==============================] - 30s 424ms/step - loss: 1.1371 - accuracy: 0.6255 - val_loss: 1.0191 - val_accuracy: 0.6687 Epoch 29/50 70/70 [==============================] - 29s 421ms/step - loss: 1.0689 - accuracy: 0.6348 - val_loss: 1.2834 - val_accuracy: 0.5688 Epoch 30/50 70/70 [==============================] - 30s 426ms/step - loss: 1.0391 - accuracy: 0.6542 - val_loss: 1.2473 - val_accuracy: 0.5813 Epoch 31/50 70/70 [==============================] - 29s 420ms/step - loss: 1.0481 - accuracy: 0.6446 - val_loss: 1.6707 - val_accuracy: 0.4521 Epoch 32/50 70/70 [==============================] - 30s 425ms/step - loss: 1.1012 - accuracy: 0.6332 - val_loss: 1.0661 - val_accuracy: 0.6292 Epoch 33/50 70/70 [==============================] - 30s 426ms/step - loss: 1.0391 - accuracy: 0.6496 - val_loss: 0.9905 - val_accuracy: 0.6500 Epoch 34/50 70/70 [==============================] - 30s 424ms/step - loss: 0.9903 - accuracy: 0.6736 - val_loss: 1.0012 - val_accuracy: 0.6521 Epoch 35/50 70/70 [==============================] - 29s 421ms/step - loss: 1.0920 - accuracy: 0.6328 - val_loss: 1.1253 - val_accuracy: 0.6229 Epoch 36/50 70/70 [==============================] - 29s 419ms/step - loss: 1.0260 - accuracy: 0.6539 - val_loss: 1.2365 - val_accuracy: 0.5896 Epoch 37/50 70/70 [==============================] - 30s 428ms/step - loss: 0.9643 - accuracy: 0.6764 - val_loss: 1.0101 - val_accuracy: 0.6896 Epoch 38/50 70/70 [==============================] - 30s 421ms/step - loss: 0.9574 - accuracy: 0.6691 - val_loss: 1.2936 - val_accuracy: 0.5688 Epoch 39/50 70/70 [==============================] - 30s 426ms/step - loss: 0.9723 - accuracy: 0.6759 - val_loss: 1.1215 - val_accuracy: 0.5792 Epoch 40/50 70/70 [==============================] - 29s 419ms/step - loss: 0.9408 - accuracy: 0.6789 - val_loss: 0.8873 - val_accuracy: 0.6708 Epoch 41/50 70/70 [==============================] - 30s 425ms/step - loss: 0.8960 - accuracy: 0.7065 - val_loss: 0.9994 - val_accuracy: 0.6771 Epoch 42/50 70/70 [==============================] - 30s 424ms/step - loss: 0.9178 - accuracy: 0.6883 - val_loss: 0.9378 - val_accuracy: 0.6729 Epoch 43/50 70/70 [==============================] - 30s 425ms/step - loss: 0.8745 - accuracy: 0.7093 - val_loss: 0.9532 - val_accuracy: 0.6812 Epoch 44/50 70/70 [==============================] - 30s 422ms/step - loss: 0.8170 - accuracy: 0.7107 - val_loss: 0.9280 - val_accuracy: 0.6896 Epoch 45/50 70/70 [==============================] - 30s 429ms/step - loss: 0.8648 - accuracy: 0.7098 - val_loss: 1.1847 - val_accuracy: 0.6208 Epoch 46/50 70/70 [==============================] - 29s 421ms/step - loss: 0.8760 - accuracy: 0.6941 - val_loss: 1.1757 - val_accuracy: 0.5875 Epoch 47/50 70/70 [==============================] - 29s 421ms/step - loss: 0.8240 - accuracy: 0.7233 - val_loss: 1.0242 - val_accuracy: 0.6458 Epoch 48/50 70/70 [==============================] - 30s 425ms/step - loss: 0.9330 - accuracy: 0.6804 - val_loss: 0.8540 - val_accuracy: 0.6979 Epoch 49/50 70/70 [==============================] - 30s 422ms/step - loss: 0.8707 - accuracy: 0.7126 - val_loss: 0.8491 - val_accuracy: 0.7083 Epoch 50/50 70/70 [==============================] - 29s 420ms/step - loss: 0.8178 - accuracy: 0.7110 - val_loss: 0.9085 - val_accuracy: 0.6833
print(colored('\x1B[1mScore','green'),score)
Score [0.6512691378593445, 0.7853403091430664]
print(colored('\x1B[1m\t Loss analysis graph of Model','green'))
plt.plot(history1.history['loss'])
plt.plot(history1.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epochs')
plt.legend(['train', 'test'])
plt.show()
Loss analysis graph of Model
print(colored('\x1B[1m\t Accuracy analysis graph of Model','green'))
plt.plot(np.array(history1.history['accuracy']) * 100)
plt.plot(np.array(history1.history['val_accuracy']) * 100)
plt.ylabel('accuracy')
plt.xlabel('epochs')
plt.legend(['train', 'validation'])
plt.title('Accuracy over epochs')
plt.show()
Accuracy analysis graph of Model
print(colored('\x1B[1mEvaluating model against test sets','blue'))
scores = cnn_sgd.evaluate(X_test_new, y_test_new)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
Evaluating model against test sets
6/6 [==============================] - 1s 91ms/step - loss: 0.6513 - accuracy: 0.7853
Test loss: 0.6512691378593445
Test accuracy: 0.7853403091430664
def plot_confusion_matrix(cm, classes, normalize=False,title='Confusion matrix',cmap=plt.cm.Greens):
fig = plt.figure(figsize=(10,10))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=90)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('Actual label')
plt.xlabel('Predicted label')
print(colored('\x1B[1mPredict the values from the test data','blue'))
y_pred = cnn_sgd.predict(X_test_new)
y_pred_Classes = np.argmax(y_pred, axis = 1)
trueY = np.argmax(y_test_new, axis = 1)
Predict the values from the test data
print(colored('\x1B[1mConfusion Matrix of Model','blue'))
confusionMTX = confusion_matrix(trueY, y_pred_Classes)
plot_confusion_matrix(confusionMTX, classes = labels.classes_)
Confusion Matrix of Model
print(colored('\x1B[1mFinal score and accuracy of the model','blue'))
score, acc = cnn_sgd.evaluate(X_test_new,y_test_new)
score1, acc1 = cnn_sgd.evaluate(X_train,y_train)
print('Test score:', score,' Test accuracy:', acc)
print('Train score:', score1,' Train accuracy:',acc1)
Final score and accuracy of the model
6/6 [==============================] - 1s 93ms/step - loss: 0.6513 - accuracy: 0.7853
120/120 [==============================] - 11s 89ms/step - loss: 0.6428 - accuracy: 0.7687
Test score: 0.6512691378593445 Test accuracy: 0.7853403091430664
Train score: 0.6428350210189819 Train accuracy: 0.768686056137085
print(colored('\x1B[1mPrecicited Label','blue'))
pred_str
Precicited Label
array(['Fat Hen'], dtype=object)
cnn_adam = Sequential()
cnn_adam.add(tf.keras.layers.InputLayer(input_shape=(64,64,3,)))
cnn_adam.add(tf.keras.layers.Conv2D(64, kernel_size=(3,3), activation='relu'))
cnn_adam.add(tf.keras.layers.MaxPool2D(pool_size = (2,2)))
cnn_adam.add(tf.keras.layers.BatchNormalization())
cnn_adam.add(tf.keras.layers.Conv2D(64, kernel_size=(3,3), strides = (1,1), activation='relu'))
cnn_adam.add(tf.keras.layers.MaxPool2D(pool_size = (2,2)))
cnn_adam.add(tf.keras.layers.BatchNormalization())
cnn_adam.add(tf.keras.layers.Conv2D(128, kernel_size=(3,3), strides = (1,1), activation='relu'))
cnn_adam.add(tf.keras.layers.MaxPool2D(pool_size = (2,2)))
cnn_adam.add(tf.keras.layers.BatchNormalization())
cnn_adam.add(tf.keras.layers.Conv2D(128, kernel_size=(3,3), strides = (1,1), activation='relu'))
cnn_adam.add(tf.keras.layers.MaxPool2D(pool_size = (2,2)))
cnn_adam.add(tf.keras.layers.GlobalMaxPool2D())
cnn_adam.add(tf.keras.layers.Flatten())
cnn_adam.add(tf.keras.layers.Dense(128, activation='relu'))
cnn_adam.add(tf.keras.layers.Dropout(0.2))
cnn_adam.add(tf.keras.layers.Dense(64, activation='relu'))
cnn_adam.add(tf.keras.layers.BatchNormalization())
cnn_adam.add(tf.keras.layers.Dense(12, activation='softmax'))
print(colored('\x1B[1mCompiling Model Using ADAM Optimizer','blue'))
opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.001, amsgrad=False)
cnn_adam.compile(optimizer = opt, loss = 'categorical_crossentropy', metrics = ['accuracy'])
Compiling Model Using ADAM Optimizer
print(colored('\x1B[1mFitting Model Using ADAM Optimizer','blue'))
Fitting Model Using ADAM Optimizer
history2 = cnn_adam.fit(augmented_train,epochs=50,shuffle=True,
steps_per_epoch = 40,
validation_data=augmented_val,
validation_steps = int(500/32))
score = cnn_adam.evaluate(X_test_new, y_test_new, verbose=0)
score_train['CNN(ADAM)'] = cnn_adam.evaluate(X_train, y_train, verbose=0)
score_test['CNN(ADAM) Test Score']=score
accuracy['CNN(ADAM) Accuracy'] = score
Epoch 1/50 40/40 [==============================] - 24s 573ms/step - loss: 2.7966 - accuracy: 0.1281 - val_loss: 3.3063 - val_accuracy: 0.1500 Epoch 2/50 40/40 [==============================] - 19s 463ms/step - loss: 2.2296 - accuracy: 0.2687 - val_loss: 2.1476 - val_accuracy: 0.3042 Epoch 3/50 40/40 [==============================] - 19s 464ms/step - loss: 1.8960 - accuracy: 0.3304 - val_loss: 1.8344 - val_accuracy: 0.3625 Epoch 4/50 40/40 [==============================] - 18s 460ms/step - loss: 1.7493 - accuracy: 0.4072 - val_loss: 2.2481 - val_accuracy: 0.3604 Epoch 5/50 40/40 [==============================] - 18s 458ms/step - loss: 1.6274 - accuracy: 0.4532 - val_loss: 1.4938 - val_accuracy: 0.4938 Epoch 6/50 40/40 [==============================] - 18s 452ms/step - loss: 1.5883 - accuracy: 0.4610 - val_loss: 1.7393 - val_accuracy: 0.4500 Epoch 7/50 40/40 [==============================] - 18s 453ms/step - loss: 1.5563 - accuracy: 0.4710 - val_loss: 1.8622 - val_accuracy: 0.3979 Epoch 8/50 40/40 [==============================] - 18s 453ms/step - loss: 1.4994 - accuracy: 0.4840 - val_loss: 1.6910 - val_accuracy: 0.3917 Epoch 9/50 40/40 [==============================] - 18s 458ms/step - loss: 1.2695 - accuracy: 0.5787 - val_loss: 1.4376 - val_accuracy: 0.5229 Epoch 10/50 40/40 [==============================] - 18s 448ms/step - loss: 1.2793 - accuracy: 0.5640 - val_loss: 1.2595 - val_accuracy: 0.5583 Epoch 11/50 40/40 [==============================] - 18s 445ms/step - loss: 1.2844 - accuracy: 0.5734 - val_loss: 1.4653 - val_accuracy: 0.4875 Epoch 12/50 40/40 [==============================] - 18s 448ms/step - loss: 1.2302 - accuracy: 0.5844 - val_loss: 1.4722 - val_accuracy: 0.5021 Epoch 13/50 40/40 [==============================] - 18s 455ms/step - loss: 1.1122 - accuracy: 0.6268 - val_loss: 1.4525 - val_accuracy: 0.5167 Epoch 14/50 40/40 [==============================] - 18s 446ms/step - loss: 1.1516 - accuracy: 0.6050 - val_loss: 1.3902 - val_accuracy: 0.5229 Epoch 15/50 40/40 [==============================] - 18s 459ms/step - loss: 1.1218 - accuracy: 0.6193 - val_loss: 1.1067 - val_accuracy: 0.6333 Epoch 16/50 40/40 [==============================] - 18s 463ms/step - loss: 1.0712 - accuracy: 0.6490 - val_loss: 1.3630 - val_accuracy: 0.5271 Epoch 17/50 40/40 [==============================] - 18s 459ms/step - loss: 0.9883 - accuracy: 0.6743 - val_loss: 0.9133 - val_accuracy: 0.6979 Epoch 18/50 40/40 [==============================] - 19s 466ms/step - loss: 0.9372 - accuracy: 0.6518 - val_loss: 1.0688 - val_accuracy: 0.6438 Epoch 19/50 40/40 [==============================] - 18s 453ms/step - loss: 0.9898 - accuracy: 0.6891 - val_loss: 0.9479 - val_accuracy: 0.6646 Epoch 20/50 40/40 [==============================] - 19s 464ms/step - loss: 1.0246 - accuracy: 0.6487 - val_loss: 0.9297 - val_accuracy: 0.6833 Epoch 21/50 40/40 [==============================] - 18s 459ms/step - loss: 0.8762 - accuracy: 0.6955 - val_loss: 1.2643 - val_accuracy: 0.5625 Epoch 22/50 40/40 [==============================] - 18s 462ms/step - loss: 0.9842 - accuracy: 0.6513 - val_loss: 0.8806 - val_accuracy: 0.6938 Epoch 23/50 40/40 [==============================] - 18s 462ms/step - loss: 0.9120 - accuracy: 0.6894 - val_loss: 0.8272 - val_accuracy: 0.7271 Epoch 24/50 40/40 [==============================] - 20s 496ms/step - loss: 0.9117 - accuracy: 0.6771 - val_loss: 1.1133 - val_accuracy: 0.6083 Epoch 25/50 40/40 [==============================] - 19s 464ms/step - loss: 0.8073 - accuracy: 0.7336 - val_loss: 0.9893 - val_accuracy: 0.6583 Epoch 26/50 40/40 [==============================] - 19s 465ms/step - loss: 0.8546 - accuracy: 0.7078 - val_loss: 1.0728 - val_accuracy: 0.6125 Epoch 27/50 40/40 [==============================] - 18s 462ms/step - loss: 0.8446 - accuracy: 0.7138 - val_loss: 0.7463 - val_accuracy: 0.7396 Epoch 28/50 40/40 [==============================] - 19s 465ms/step - loss: 0.7573 - accuracy: 0.7295 - val_loss: 0.8089 - val_accuracy: 0.7063 Epoch 29/50 40/40 [==============================] - 19s 467ms/step - loss: 0.8174 - accuracy: 0.7274 - val_loss: 1.0660 - val_accuracy: 0.6313 Epoch 30/50 40/40 [==============================] - 18s 461ms/step - loss: 0.7840 - accuracy: 0.7357 - val_loss: 0.7414 - val_accuracy: 0.7292 Epoch 31/50 40/40 [==============================] - 18s 449ms/step - loss: 0.7384 - accuracy: 0.7442 - val_loss: 1.0038 - val_accuracy: 0.6500 Epoch 32/50 40/40 [==============================] - 19s 465ms/step - loss: 0.7251 - accuracy: 0.7620 - val_loss: 0.8133 - val_accuracy: 0.7083 Epoch 33/50 40/40 [==============================] - 19s 470ms/step - loss: 0.7052 - accuracy: 0.7533 - val_loss: 0.8014 - val_accuracy: 0.7542 Epoch 34/50 40/40 [==============================] - 18s 461ms/step - loss: 0.7120 - accuracy: 0.7796 - val_loss: 0.9761 - val_accuracy: 0.6938 Epoch 35/50 40/40 [==============================] - 19s 480ms/step - loss: 0.7021 - accuracy: 0.7609 - val_loss: 0.8480 - val_accuracy: 0.6917 Epoch 36/50 40/40 [==============================] - 18s 457ms/step - loss: 0.6562 - accuracy: 0.7702 - val_loss: 0.6363 - val_accuracy: 0.7792 Epoch 37/50 40/40 [==============================] - 18s 452ms/step - loss: 0.6583 - accuracy: 0.7827 - val_loss: 0.9758 - val_accuracy: 0.6625 Epoch 38/50 40/40 [==============================] - 19s 464ms/step - loss: 0.7659 - accuracy: 0.7389 - val_loss: 1.1262 - val_accuracy: 0.6083 Epoch 39/50 40/40 [==============================] - 18s 460ms/step - loss: 0.7147 - accuracy: 0.7530 - val_loss: 0.6087 - val_accuracy: 0.7937 Epoch 40/50 40/40 [==============================] - 18s 456ms/step - loss: 0.7505 - accuracy: 0.7507 - val_loss: 0.8719 - val_accuracy: 0.6917 Epoch 41/50 40/40 [==============================] - 18s 447ms/step - loss: 0.7648 - accuracy: 0.7203 - val_loss: 0.6236 - val_accuracy: 0.7875 Epoch 42/50 40/40 [==============================] - 18s 448ms/step - loss: 0.6642 - accuracy: 0.7653 - val_loss: 0.8757 - val_accuracy: 0.6979 Epoch 43/50 40/40 [==============================] - 18s 445ms/step - loss: 0.6301 - accuracy: 0.7844 - val_loss: 0.6644 - val_accuracy: 0.7625 Epoch 44/50 40/40 [==============================] - 18s 446ms/step - loss: 0.7278 - accuracy: 0.7587 - val_loss: 0.8367 - val_accuracy: 0.7083 Epoch 45/50 40/40 [==============================] - 18s 457ms/step - loss: 0.6007 - accuracy: 0.7971 - val_loss: 0.6185 - val_accuracy: 0.7708 Epoch 46/50 40/40 [==============================] - 18s 454ms/step - loss: 0.5511 - accuracy: 0.8067 - val_loss: 0.7673 - val_accuracy: 0.7354 Epoch 47/50 40/40 [==============================] - 18s 446ms/step - loss: 0.6566 - accuracy: 0.7742 - val_loss: 0.6329 - val_accuracy: 0.7812 Epoch 48/50 40/40 [==============================] - 18s 456ms/step - loss: 0.5850 - accuracy: 0.7996 - val_loss: 0.7346 - val_accuracy: 0.7458 Epoch 49/50 40/40 [==============================] - 18s 456ms/step - loss: 0.5862 - accuracy: 0.7985 - val_loss: 0.5892 - val_accuracy: 0.7937 Epoch 50/50 40/40 [==============================] - 18s 452ms/step - loss: 0.6033 - accuracy: 0.7966 - val_loss: 0.4527 - val_accuracy: 0.8354
print(colored('\x1B[1mScore','green'),score)
Score [0.3903430998325348, 0.8638743162155151]
print(colored('\x1B[1m\t Loss analysis graph of Model','green'))
plt.plot(history2.history['loss'])
plt.plot(history2.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epochs')
plt.legend(['train', 'test'])
plt.show()
Loss analysis graph of Model
print(colored('\x1B[1m\t Accuracy analysis graph of Model','green'))
plt.plot(np.array(history2.history['accuracy']) * 100)
plt.plot(np.array(history2.history['val_accuracy']) * 100)
plt.ylabel('accuracy')
plt.xlabel('epochs')
plt.legend(['train', 'validation'])
plt.title('Accuracy over epochs')
plt.show()
Accuracy analysis graph of Model
print(colored('\x1B[1mEvaluating model against test sets','blue'))
scores = cnn_adam.evaluate(X_test_new, y_test_new)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
Evaluating model against test sets
6/6 [==============================] - 1s 90ms/step - loss: 0.3903 - accuracy: 0.8639
Test loss: 0.3903430998325348
Test accuracy: 0.8638743162155151
def plot_confusion_matrix(cm, classes, normalize=False,title='Confusion matrix',cmap=plt.cm.Greens):
fig = plt.figure(figsize=(10,10))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=90)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('Actual label')
plt.xlabel('Predicted label')
print(colored('\x1B[1mPredict the values from the test data','blue'))
y_pred = cnn_adam.predict(X_test_new)
y_pred_Classes = np.argmax(y_pred, axis = 1)
trueY = np.argmax(y_test_new, axis = 1)
Predict the values from the test data
print(colored('\x1B[1mConfusion Matrix of Model','blue'))
confusionMTX = confusion_matrix(trueY, y_pred_Classes)
plot_confusion_matrix(confusionMTX, classes = labels.classes_)
Confusion Matrix of Model
print(colored('\x1B[1mFinal score and accuracy of the model','blue'))
score, acc = cnn_adam.evaluate(X_test_new,y_test_new)
score1, acc1 = cnn_adam.evaluate(X_train,y_train)
print('Test score:', score,' Test accuracy:', acc)
print('Train score:', score1,' Train accuracy:',acc1)
#Model with training accuracy of 86% while testing accuracy 84%.
Final score and accuracy of the model
6/6 [==============================] - 1s 94ms/step - loss: 0.3903 - accuracy: 0.8639
120/120 [==============================] - 11s 91ms/step - loss: 0.4405 - accuracy: 0.8403
Test score: 0.3903430998325348 Test accuracy: 0.8638743162155151
Train score: 0.4404798448085785 Train accuracy: 0.840283215045929
print(colored('\x1B[1mPredection using test folder images','blue'))
test_images_path= '/content/drive/MyDrive/Files/Part !/Part-1 - Plant Seedling Classification Data/Seedling - Prediction/*.png'
test_images = glob(test_images_path)
test_images_arr = []
print(colored('\x1B[1mGetting the images from test folder','green'))
for img in test_images:
test_images_arr.append(cv2.resize(cv2.imread(img), (64, 64)))
test_X = np.asarray(test_images_arr)
print(colored('\x1B[1mNormalizing the Images','green'))
test_X = test_X.astype('float32') / 255
Predection using test folder images Getting the images from test folder Normalizing the Images
print('''\n\033[1m''' + '''Displaying and comparing all the models designed with their train and test accuracies''' + '''\033[0m''')
results = pd.DataFrame({'Model' : score_train.keys(),
'Train Score' : score_train.values(),
'Test Score': score_test.values(),
'Accuracy' : accuracy.values()})
results
Displaying and comparing all the models designed with their train and test accuracies
| Model | Train Score | Test Score | Accuracy | |
|---|---|---|---|---|
| 0 | SVM | 0.996103 | 0.499651 | 0.499651 |
| 1 | Random Forest | 0.958933 | 0.427673 | 0.427673 |
| 2 | Decision Tree | 0.264388 | 0.22362 | 0.22362 |
| 3 | NN(SGD) | [0.9962068796157837, 0.6698138117790222] | [1.4598736763000488, 0.5073375105857849] | [1.4598736763000488, 0.5073375105857849] |
| 4 | NN(ADAM) | [0.35462790727615356, 0.9045370817184448] | [1.6843140125274658, 0.5094339847564697] | [1.6843140125274658, 0.5094339847564697] |
| 5 | CNN(SGD) | [0.6428350210189819, 0.768686056137085] | [0.6512691378593445, 0.7853403091430664] | [0.6512691378593445, 0.7853403091430664] |
| 6 | CNN(ADAM) | [0.4404798448085785, 0.840283215045929] | [0.3903430998325348, 0.8638743162155151] | [0.3903430998325348, 0.8638743162155151] |
print(colored('\x1B[1mSaving Model:','blue'))
cnn_adam.save('CNN_ADAM Classification')
Saving Model:
INFO:tensorflow:Assets written to: CNN_ADAM Classification/assets
print(colored('\x1B[1mPredection using test folder images','blue'))
test_images_path= '/content/drive/MyDrive/Files/Part !/Part-1 - Plant Seedling Classification Data/Seedling - Prediction/*.png'
test_images = glob(test_images_path)
test_images_arr = []
print(colored('\x1B[1mGetting the images from test folder','green'))
for img in test_images:
test_images_arr.append(cv2.resize(cv2.imread(img), (64, 64)))
test_X = np.asarray(test_images_arr)
print(colored('\x1B[1mNormalizing the Images','green'))
test_X = test_X.astype('float32') / 255
Predection using test folder images Getting the images from test folder Normalizing the Images
print(colored('\x1B[1mPredicting using CNN_ADAM model','blue'))
predictions = cnn_adam.predict(test_X)
preds = np.argmax(predictions, axis=1)
pred_str = labels.classes_[preds]
pred_str
Predicting using CNN_ADAM model
array(['Loose Silky-bent'], dtype=object)
print(colored('\x1B[1mPredicting using CNN_SGD model','blue'))
predictions = cnn_sgd.predict(test_X)
preds = np.argmax(predictions, axis=1)
pred_str = labels.classes_[preds]
pred_str
Predicting using CNN_SGD model
array(['Fat Hen'], dtype=object)